<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Activity as ActivityModel;
use app\common\model\ActivityItem;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 下拉框数据集合控制器
 * @package app\index\controller
 */
class Select extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取房间列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_room()
    {
        $roomData = Db::name('qt_room')
            ->field('id,name')
            ->where(['store_id' => $this->userInfo['store_id'], 'is_deleted' => '2'])
            ->select();
        $this->result('查询成功', $roomData, 200);
    }


    /**
     * 获取手牌列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_head_type()
    {
        $roomData = Db::name('qt_hand_card_type')->field('id,name')->select();
        $this->result('查询成功', $roomData, 200);
    }


    /**
     * 获取一次性收费列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_goods()
    {
        $roomData = Db::name('qt_goods')
            ->field('id,name,price')
            ->where('store_id', $this->userInfo['store_id'])
            ->select();
        $this->result('查询成功', $roomData, 200);
    }


    /**
     * 获取技师列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_engineer()
    {
        $post = $this->request->post();

        $status = isset($post['status']) ? $post['status'] : 0;
        $query = Db::name('qt_users')
            ->field('id,real_name,work_number')
            ->where('store_id', $this->userInfo['store_id']);
        if (!empty($post['group'])) {
            if ($post['group'] == 1) {
                $query->where('group', '1');
            }
            if ($post['group'] == 2) {
                $query->where('group', '2');
            }
        } else {
            $query->where('role', '3');
        }
        $query->where('status', $status);

        $userData = $query->select();
        $this->result('查询成功', $userData, 200);
    }


    /**
     * 获取项目列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_project()
    {
        $roomData = Db::name('qt_project')
            ->field('id,name,price,queue_block_rate')
            ->where(['store_id'=>$this->userInfo['store_id'],'status'=>'1'])
            ->select();
        $this->result('查询成功', $roomData, 200);
    }


    /**
     * 获取备注类型
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_remarks_type()
    {
        $typeData = Db::name('qt_remarks_type')->field('id,name')->select();
        $this->result('查询成功', $typeData, 200);
    }


    /**
     * 获取备注类型
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_company()
    {
        $typeData = Db::name('qt_charge_company')->field('id,company_name')->select();
        $this->result('查询成功', $typeData, 200);
    }


    /**
     * 获取所有项目商品
     */
    public function get_project_goods()
    {
        $goods = Db::name('qt_goods')->field('id,name,price')->where('store_id', $this->userInfo['store_id'])->select();
        foreach ($goods as $key => $value) {
            $goods[$key]['id'] = 'goods' . '-' . $value['id'];
            $goods[$key]['item_type'] = 'goods';
        }
        $project = Db::name('qt_project')->field('id,name,price')->where('store_id', $this->userInfo['store_id'])->select();
        foreach ($project as $key => $value) {
            $project[$key]['id'] = 'project' . '-' . $value['id'];
            $project[$key]['item_type'] = 'project';
        }
        $data = array_merge($project, $goods);
        $this->result('查询成功', $data, 200);
    }


    /**
     * 获取会员类型下拉接口
     */
    public function get_viper_type()
    {
        if ($this->request->isPost()) {
            $list = Db::name('qt_viper_type')
                ->where(['store_id' => $this->userInfo['store_id']])
                ->field('id,name')->select();
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取用户工种
     */
    public function get_work_type()
    {
        if ($this->request->isPost()) {
            static $tree = [];
            $list = Db::name('qt_work_type')
                ->where(['store_id' => $this->userInfo['store_id'], 'pid' => '0'])
                ->field('id,title')
                ->select();
            foreach ($list as $key => $value) {
                $tree[] = $value;
                $child = Db::name('qt_work_type')
                    ->where(['store_id' => $this->userInfo['store_id'], 'pid' => $value['id']])
                    ->field('id,title')
                    ->select();
                foreach ($child as $k => $val) {
                    $tree[] = $val;
                }
            }
            $this->result('获取成功', $tree, 200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取用户工种
     */
    public function get_top_work_type()
    {
        if ($this->request->isPost()) {
            static $tree = [];
            $list = Db::name('qt_work_type')
                ->where(['store_id' => $this->userInfo['store_id'], 'pid' => '0'])
                ->field('id,title')
                ->select();
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取积分兑换模板
     */
    public function get_integral_tmp()
    {
        if ($this->request->isPost()) {
            $list = Db::name('qt_viper_integral_exchange')
                ->where(['store_id' => $this->userInfo['store_id']])
                ->field('id,name')
                ->select();
            $this->result('获取成功', $list);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取岗位
     */
    public function get_post()
    {
        if ($this->request->isPost()) {
            $list = Db::name('qt_user_role')
                ->where(['store_id' => $this->userInfo['store_id']])
                ->field('id,name as title')
                ->select();
            $this->result('获取成功', $list);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取门店
     */
    public function get_store()
    {
        if ($this->request->isPost()) {
            $list = Db::name('qt_user_store')
                ->alias('us')
                ->leftJoin('qt_store s','s.id  = us.store_id')
                ->field('s.id,s.name')
                ->where(['us.user_id'=>$this->userInfo['id']])
                ->select();
            $this->result('获取成功', $list);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取会员卡充值模板信息
     */
    public function get_recharge_template()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $list = Db::name('qt_viper_recharge_template')
                ->where('viper_type_id', $getData['viper_type_id'])
                ->field('id,name,amount,give_amount')
                ->select();
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    /**
     * 获取会员卡次卡充值模板
     */
    public function get_number_template()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $list = Db::name('qt_viper_number_card_template')
                ->where('viper_type_id', $getData['viper_type_id'])
                ->field('id,name,amount')
                ->select();
            $this->result('获取成功', $list);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    /**
     * 获取会员卡月卡充值模板
     */
    public function get_month_template()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $list = Db::name('qt_viper_month_card_template')
                ->where('viper_type_id', $getData['viper_type_id'])
                ->field('id,name,amount')
                ->select();
            $this->result('获取成功', $list);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取操作类型
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_user_menu()
    {
        if ($this->request->isPost()) {
            $list = Db::name('qt_user_menu')
                ->where('store_id', $this->userInfo['store_id'])
                ->field('id,title')
                ->select();
            $this->result('获取成功', $list);
        } else {
            $this->result('请求错误', '', 204);
        }
    }


    /**
     * 获取设备类型
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_equipment()
    {
        if ($this->request->isPost()) {
            $list = Db::name('qt_equipment')
                ->field('id,name')
                ->select();
            $this->result('获取成功', $list);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    /**
     * 获取在线技师列表
     */
    public function get_engineer_online()
    {

        $post = $this->request->post();
        $query = Db::name('qt_users')
            ->field('id,real_name,work_number')
            ->where('store_id', $this->userInfo['store_id']);
        if (!empty($post['group'])) {
            if ($post['group'] == 1) {
                $query->where('group', '1')->where('role', '3');
            }
            if ($post['group'] == 2) {
                $query->where('group', '2');
            }
        } else {
            $query->where('role', '3');
        }

        $userData = $query->select();
        $this->result('查询成功', $userData, 200);
    }


    /**
     * 获取房间类型+
     */
    public function getRoomType()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            static $tree = [];
            $data = Db::name('qt_room_type')
                ->field('id,name')
                ->where(['store_id' => $userInfo['store_id'], 'status' => '1', 'pid' => '0'])
                ->order('sort asc')
                ->select();
            foreach ($data as $key => $value) {
                $tree[] = $value;
                $child = Db::name('qt_room_type')
                    ->where(['store_id' => $this->userInfo['store_id'], 'pid' => $value['id']])
                    ->field('id,name')
                    ->select();
                foreach ($child as $k => $val) {
                    $tree[] = $val;
                }

            }
            $this->result('查询成功', $tree, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取房间类型
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProjectType()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            static $tree = [];
            $data = Db::name('qt_project_category')
                ->field('id,name,pid')
                ->where(['store_id' => $userInfo['store_id'], 'pid' => '0', 'status' => '1'])
                ->order('sort asc')
                ->select();
            foreach ($data as $key => $value) {
                $tree[] = $value;
                $child = Db::name('qt_project_category')
                    ->where(['store_id' => $this->userInfo['store_id'], 'pid' => $value['id'], 'status' => '1'])
                    ->field('id,name,pid')
                    ->select();
                foreach ($child as $k => $val) {
                    $tree[] = $val;
                }

            }
            $this->result('查询成功', $tree, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取一次性收费列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_all_goods()
    {
        $roomData = Db::name('qt_goods')
            ->field('id,name,price')
            ->where('store_id', $this->userInfo['store_id'])
            ->select();
        $this->result('查询成功', $roomData, 200);
    }

    /**
     * 活动列表
     * @return mixed
     * @throws \think\Exception
     */
    public function get_activity_list()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data_list = ActivityModel::field('id,name')
                ->where(['store_id' => $userInfo['store_id']])
                ->order("id desc")->select();
            $this->result('查询成功', $data_list, 200);
        }
        $this->result('无效的请求', '', 204);

    }


    /**
     * 获取房间订房规则
     */
    public function getRoomRule()
    {
        if ($this->request->isPost()) {
            $roomId = $this->request->post('room_id');
            $data = Db::name('qt_room')->field('id,name,book_room')->where('id', $roomId)->find();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 获取技师的项目
     */
    public function get_engineer_project()
    {
        $getData = $this->request->post();
        if (empty($getData['engineer_id'])) {
            $this->result('缺少参数', '', 204);
        }
        $rule = Db::name('qt_users')->where(['id' => $getData['engineer_id']])->value('add_clock_project');
        if ($rule == '1') {
            $data = Db::name('qt_user_project')
                ->field('p.id,p.name as project_name,p.price')
                ->alias('up')
                ->leftJoin('qt_project p', 'p.id = up.project_id')
                ->where(['up.engineer_id' => $getData['engineer_id'], 'p.store_id' => $this->userInfo['store_id'],'p.status'=>'1'])
                ->select();
        } else {
            $data = Db::name('qt_project')
                ->field('id,name as project_name,price')
                ->where('store_id', $this->userInfo['store_id'])
                ->select();
        }
        $this->result('查询成功', $data, 200);
    }


    /**
     * 获取多种排位列表
     */
    public function get_many_queue()
    {
        $data = Db::name('qt_many_queue_rule')
            ->field('id as many_queue_id,rule_name')
            ->where(['store_id' => $this->userInfo['store_id']])
            ->select();
        $this->result('查询成功', $data, 200);
    }

    /**
     * 获取角色名称
     */
    public function get_role()
    {
        $data = Db::name('qt_user_role')
            ->field('id,name')
            ->where(['store_id' => $this->userInfo['store_id'], 'status' => '1'])
            ->select();
        $this->result('查询成功', $data, 200);
    }


    public function filter_Emoji($str)
    {
        $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str
        );

        return $str;

    }













}
